﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Rung.Common;

namespace Rung.EBook.WebSiteAnalizer.Analizer
{
    public static class AnalizerFactory
    {
        public static void SaveAnalizers(List<AnalizerBase> mAnalizers)
        {
            string fullName = Rung.EBook.Common.Driver.StaticFunctions.GeAnalizersFilePath();
            SerializeHelper.Serialize(mAnalizers, fullName);
        }

        public static List<AnalizerBase> GetAnalizers()
        {
            //string fullName = Rung.EBook.Common.Driver.StaticFunctions.GeAnalizersFilePath();
            //return SerializeHelper.DeSerialize<List<AnalizerBase>>(fullName);

            /* 以下代码作废（可以用作重新生成持久化文件） ------------------------------------------- */

            List<AnalizerBase> result = new List<AnalizerBase>();

            #region 毛泽东文集 - Books
            AnalizerFactory.CheckExist(result, "毛泽东文集");

            result.Add(new AnalizerBase
            {
                Name = "河南共青团",
                BookNamePrefix = "毛泽东文集",
                Encoding = Encoding.GetEncoding("GB2312"),
                SiteUrl = "http://www.hnyouth.org.cn/columns/zlwx.asp",
                BooksUrl = "http://www.hnyouth.org.cn/columns/zlwx.asp",
                BookRegex = "<a href=\"http://cpc.people.com.cn/GB/69112/70190/70197/(?<url>\\d{5})/index.html\">(?<title>.+?)</a>(<br>|</td>)",
                ChaptersUrl = "http://cpc.people.com.cn/GB/69112/70190/70197/{0}/index.html",//章节列表
                ChapterRegex = "·<a href='/GB/69112/70190/70197/{0}/(?<url>\\d{7}).html' target=_blank class=\"a14blk1\">(?<title>.+?)</a><br>",//章节正则（包含前言）
                ContentBaseUrl = "http://cpc.people.com.cn/GB/69112/70190/70197/{0}/{1}.html",//正文页面
                ContentRegex = "(?<=<td class=\"t14blu1\" id=zoom>).+?(?=</p></td>)"//正文正则 
            });
            #endregion

            #region 说岳全传
            if (result.Find(delegate(AnalizerBase bm) { return bm.Name == "竹语清梦"; }) != null) throw (new Exception("已存在名为【竹语清梦】的解析器。"));

            result.Add(new AnalizerBase
            {
                Name = "竹语清梦",
                BookNamePrefix = "说岳全传",
                SiteUrl = "http://blog.guqu.net/user1/14/index.html",
                Encoding = Encoding.GetEncoding("GB2312"),
                ChaptersUrl = "http://blog.guqu.net/user1/14/archives/2006/1936.html",//章节列表
                ChapterRegex = "(?<=<TD WIDTH=\"(47|44)%\">([\\n\\r]*<CENTER>){0,1}<A\\s+(style=\"TEXT-DECORATION: none\"){0,1} HREF=\"(http://www.my285.com/gdwx/xs/xy/syqz/){0,1}(?<url>\\d+).htm\">(<FONT size=2>){0,1})(?<title>[\\w\\s]+)(?=(</FONT>){0,1}</A>(</CENTER>[\\n\\r]*){0,1}</TD>)",//章节正则（包含前言）
                ChapterRegexTag = new string[] { "url", "title" },
                ContentBaseUrl = "http://www.my285.com/gdwx/xs/xy/syqz/{0}.htm",//正文页面
                ContentRegex = "(?<=<hr color=\"#EE9B73\" size=\"1\" width=\"94%\">)(?<content>.*)(?=<br>--------------------<br>)"//正文正则 
            });
            #endregion

            #region 亮剑
            if (result.Find(delegate(AnalizerBase bm) { return bm.Name == "小说库"; }) != null) throw (new Exception("已存在名为【小说库】的解析器。"));

            result.Add(new AnalizerBase
            {
                Name = "小说库",
                BookNamePrefix = "亮剑",
                SiteUrl = "http://www.xiaoshuoku.com.cn/",
                Encoding = Encoding.GetEncoding("GB2312"),
                ChaptersUrl = "http://www.xiaoshuoku.com.cn/sub_list/7205.html",//章节列表
                ChapterRegex = "<a href=\"../detail33/(?<url>\\d+).html\" class='lv' target='_self' title='(?<title>\\w+)'>",//章节正则（包含前言）
                ChapterRegexTag = new string[] { "url", "title" },
                ContentBaseUrl = "http://www.xiaoshuoku.com.cn/detail33/{0}.html",//正文页面
                ContentRegex = "(?<=<div class=\"last_title1 \">&nbsp;&nbsp;&nbsp;&nbsp;　　<br>)(?<content>.*?)(?=</div>)"//正文正则 
            });
            #endregion

            #region 金庸小说集 - Books
            AnalizerFactory.CheckExist(result, "今日之家 免费读书网");

            result.Add(new AnalizerBase
            {
                Name = "今日之家 免费读书网",
                BookNamePrefix = "金庸小说集",
                Encoding = Encoding.GetEncoding("GB2312"),
                SiteUrl = "http://www.jrzj.org/wxxs/j/jingyong/index.html",
                BooksUrl="http://www.jrzj.org/wxxs/j/jingyong/index.html",
                BookRegex = "<a HREF=\"(?<url>.+?)/index.html\"  ><FONT SIZE=\"\" COLOR=\"red\">。</FONT>(?<title>.+?)</A>",
                ChaptersUrl = "http://www.jrzj.org/wxxs/j/jingyong/{0}/index.html",//章节列表
                ChapterRegex = "<td WIDTH=\"\\d{2}%\">\\s*(<center>){0,1}<a href=\"(?<url>\\d{3}).htm\"  >(?<title>.+?)</a>(</center>){0,1}\\s*</td>",//章节正则（包含前言）
                ChapterRegexTag = new string[] { "url", "title" },
                ContentBaseUrl = "http://www.jrzj.org/wxxs/j/jingyong/{0}/{1}.htm",//正文页面
                ContentRegex = "(?<=<hr color=\"#EE9B73\" size=\"1\" width=\"94%\">\\s*).*?(?=<hr color=\"#EE9B73\" size=\"1\" width=\"94%\">)"//正文正则 
            });
            #endregion

            #region 黑道风云二十年 - 换一个网址
            if (result.Find(delegate(AnalizerBase bm) { return bm.Name == "幻想天空"; }) != null) throw (new Exception("已存在名为【幻想天空】的解析器。"));

            result.Add(new AnalizerBase
            {
                Name = "幻想天空",
                BookNamePrefix = "黑道风云二十年",
                SiteUrl = "http://www.hxtk.com/book/36959/content.html",
                Encoding = Encoding.Default,
                ChaptersUrl = "http://www.hxtk.com/book/36959/content.html",//章节列表
                ChapterRegex = "<td><A href='/book/36959/(?<url>\\d+).html' title='添加时间：\\d{4}-\\d{1,2}-\\d{1,2} \\d{1,2}:\\d{2}:\\d{2}'>(?<title>.*?)</A></td>",//章节正则（包含前言）
                ChapterRegexTag = new string[] { "url", "title" },
                ContentBaseUrl = "http://www.hxtk.com/book/36959/{0}.html",//正文页面
                ContentRegex = "(?<=<div class=\"chaptercontent\">).*?(?=[,|\\.]+</div>)"//正文正则 
            });
            #endregion

            #region 易读（商道）
            AnalizerFactory.CheckExist(result, "易读（商道）");

            result.Add(new AnalizerBase
            {
                Name = "易读（商道）",
                BookNamePrefix = "",
                Encoding = Encoding.GetEncoding("gb2312"),
                SiteUrl = "http://www.yi-look.com/",
                BooksUrl = "http://www.yi-look.com/artc_6.html",
                BookRegex = "<DIV Class=T1><a href='(?<url>.+?).html' target='_blank' >《(?<title>.+?)》</A></DIV>",
                ChaptersUrl = "http://www.yi-look.com/{0}.html",//章节列表
                ChapterRegex = "<a href='read_(?<url>.+?).html' >(?<title>.+?)</A>",//章节正则（包含前言）
                ChapterRegexTag = new string[] { "url", "title" },
                ContentBaseUrl = "http://www.yi-look.com/read_{1}.html",//正文页面
                ContentRegex = "(?<=<TR><TD CLASS=ART>\\s).*?(?=<p>)"//正文正则 
            });
            #endregion

            #region 名书楼 - 决战朝鲜
            if (result.Find(delegate(AnalizerBase bm) { return bm.Name == "名书楼"; }) != null) throw (new Exception("已存在名为【名书楼】的解析器。"));

            result.Add(new AnalizerBase
            {
                Name = "名书楼",
                BookNamePrefix = "决战朝鲜",
                SiteUrl = "http://www.mingshulou.com",
                Encoding = Encoding.GetEncoding("gb2312"),
                //ChaptersUrl = "http://www.mingshulou.com/a/1/1103/",//章节列表
                ChaptersUrl = "http://www.78xs.com/article/19/170/Default.shtml",//章节列表
                //ChapterRegex = "<a href=\"(?<url>\\d+?).html\" title=\".+?\">(?<title>.+?)</a>",//章节正则（包含前言）
                ChapterRegex = @"<a href=""(?<url>\d+?).shtml"" title=""更新时间:.+?\s*更新字数:\d+"">(?<title>.+?)</a>",//章节正则（包含前言）
                ChapterRegexTag = new string[] { "url", "title" },
                //ContentBaseUrl = "http://www.mingshulou.com/a/1/1103/{0}.html",//正文页面
                ContentBaseUrl = "http://www.78xs.com/article/19/170/{0}.shtml",//正文页面
                ContentRegex = "(?<=<script src=\"http://cpro.baidu.com/cpro/ui/c.js\" type=\"text/javascript\"></script>).+?(?=</div>)",//正文正则
                //ContentRegex = "(?<=<div id=\"chapter_content\">).+?(?=</div>)",//正文正则
                ContentClears = new Dictionary<string, string> 
                {
                    {"&nbsp;&nbsp;&nbsp;&nbsp;",    ""},
                    {"更多到，地址",    ""},
                    {"http://www.wjxsw.com",    ""}
                }
            });
            #endregion

            #region 那天她敲了我宿舍的门
            if (result.Find(delegate(AnalizerBase bm) { return bm.Name == "红袖添香"; }) != null) throw (new Exception("已存在名为【红袖添香】的解析器。"));

            result.Add(new AnalizerBase
            {
                Name = "文学库",
                BookNamePrefix = "那天她敲了我宿舍的门",
                SiteUrl = "http://novel.hongxiu.com/a/37598/list.shtml",
                Encoding = Encoding.UTF8,
                ChaptersUrl = "http://novel.hongxiu.com/a/37598/list.shtml",//章节列表
                ChapterRegex = "<li><strong\\s*>\\d+.\\s*<a (title=[\"|']+(?<title>.+?)[\"|']+?|target=[\"|']+_blank[\"|']+?|href=[\"|']+(?<url>\\d+?).shtml[\"|']+?|\\s?){5,6}?>",//章节正则（包含前言）
                ChapterRegexTag = new string[] { "url", "title" },
                ContentBaseUrl = "http://novel.hongxiu.com/a/37598/{0}.shtml",//正文页面
                ContentRegex = "(?<=<div id=\"content\">).+?(?=</div>)"//正文正则 
            });
            #endregion

            #region 都市风水师
            if (result.Find(delegate(AnalizerBase bm) { return bm.Name == "三界中文网"; }) != null) throw (new Exception("已存在名为【三界中文网】的解析器。"));

            result.Add(new AnalizerBase
            {
                Name = "三界中文网",
                BookNamePrefix = "都市风水师",
                SiteUrl = "http://www.3jzw.com/files/article/html/6/6121/",
                Encoding = Encoding.GetEncoding("gb2312"),
                ChaptersUrl = "http://www.3jzw.com/files/article/html/6/6121/",//章节列表
                ChapterRegex = "<td class=\"ccss\">\\s*<a href=\"(?<url>\\d{7}).html\">(?<title>.+?)</a>\\s*</td>",//章节正则（包含前言）
                ChapterRegexTag = new string[] { "url", "title" },
                ContentBaseUrl = "http://www.3jzw.com/files/article/html/6/6121/{0}.html",//正文页面
                ContentRegex = "(?<=<div id=\"content\">).+?(?=(<br( /)?>)*(&nbsp;|\\s)*(\\(\\)<br /><br />)*((&nbsp;|\\s)*随时随地享受阅读的乐趣!)*(<br( /)?>)*(<center>)*欢迎您访问三界中文)"//正文正则 
            });
            #endregion

            #region 远东朝鲜战争
            if (result.Find(delegate(AnalizerBase bm) { return bm.Name == "天涯在线书库 - 军事"; }) != null) throw (new Exception("已存在名为【天涯在线书库 - 军事】的解析器。"));

            result.Add(new AnalizerBase
            {
                Name = "天涯在线书库 - 军事",
                BookNamePrefix = "",
                Encoding = Encoding.GetEncoding("gb2312"),
                SiteUrl = "http://www.tianyabook.com/junshi.htm",
                BooksUrl = "http://www.tianyabook.com/junshi.htm",
                BookRegex = "<td width=3[3|4]%\\s+height=22>\\s*<a href=\"junshi/(?<url>.+?).html{0,1}\">(?<title>.+?)</a>",
                ChaptersUrl = "http://www.tianyabook.com/junshi/{0}.htm",//章节列表
                ChapterRegex = "<td width=\"20%\">\\s*<a href=\"(?<url>.+?).htm\">(?<title>.+?)</a></td>",//章节正则（包含前言）
                ChapterRegexTag = new string[] { "url", "title" },
                ContentBaseUrl = "http://www.tianyabook.com/junshi/{0}/{1}.htm",//正文页面
                ContentRegex = "(?<=<hr width=\"94%\" color=\"#f5f8f8\" SIZE=\"1\">\\s*<br>).+?(?=\\s*<p></td>)"//正文正则 
            });
            #endregion

            #region 凤凰网 - 时政
            if (result.Find(delegate(AnalizerBase bm) { return bm.Name == "纵横-军事"; }) != null) throw (new Exception("已存在名为【纵横-军事】的解析器。"));

            result.Add(new AnalizerBase
            {
                Name = "纵横-军事",
                BookNamePrefix = "",
                Encoding = Encoding.GetEncoding("GB2312"),
                SiteUrl = "http://www.zongheng.com/",
                BooksUrl = "http://book.zongheng.com/store/c6/c0/u0/p1/v0/ALL.html",
                BookRegex = "<dt style=\"font-size:14px; line-height:30px; font-weight:bold; height:30px; overflow:hidden;\"><a href=\"/read/book/book_info/(?<url>.+?).htm\" target=\"_blank\">(?<title>.+?)</a></dt>",
                ChaptersUrl = "http://v.book.ifeng.com/read/book/book_info/{0}.htm",//章节列表
                ChapterRegex = "<a href=\"/read/book/chapter_info/13829/(?<url>.+?).htm\">\\s*(?<title>.+?)</a>",//章节正则（包含前言）
                ChapterRegexTag = new string[] { "url", "title" },
                ContentBaseUrl = "http://v.book.ifeng.com/read/book/chapter_info/13829/{0}.htm",//正文页面
                ContentRegex = "(?<=<hr width=\"94%\" color=\"#f5f8f8\" SIZE=\"1\">\\s*<br>).+?(?=\\s*<p></td>)"//正文正则 
            });
            #endregion

            #region 凤凰网 - 时政
            if (result.Find(delegate(AnalizerBase bm) { return bm.Name == "凤凰网-军事"; }) != null) throw (new Exception("已存在名为【凤凰网-军事】的解析器。"));

            result.Add(new AnalizerBase
            {
                Name = "凤凰网-军事",
                BookNamePrefix = "",
                Encoding = Encoding.UTF8,
                SiteUrl = "http://www.ifeng.com/",
                BooksUrl = "http://v.book.ifeng.com/read/book/category_info.htm?categoryId=436&currPage=2",
                BookRegex = "<dt style=\"font-size:14px; line-height:30px; font-weight:bold; height:30px; overflow:hidden;\"><a href=\"/read/book/book_info/(?<url>.+?).htm\" target=\"_blank\">(?<title>.+?)</a></dt>",
                ChaptersUrl = "http://v.book.ifeng.com/read/book/book_info/{0}.htm",//章节列表
                ChapterRegex = "<a href=\"/read/book/chapter_info/{0}/(?<url>.+?).htm\">\\s*(?<title>.+?)</a>",//章节正则（包含前言）
                ChapterRegexTag = new string[] { "url", "title" },
                Method = Rung.EBook.WebSiteAnalizer.Driver.HttpMethod.Post,
                ContentBaseUrl = "http://v.book.ifeng.com/read/book/remc.htm?c={1}&b={0}",//正文页面
                ContentRegex = "(?<={\"content\":\").+?(?=\"})",//正文正则
                ContentClears = new Dictionary<string, string> {// 正文中要清理的垃圾
                    {@"\\r", ""}
                }
            });
            #endregion

            #region 108小说网 - 军事
            if (result.Find(delegate(AnalizerBase bm) { return bm.Name == "108小说网-军事"; }) != null) throw (new Exception("已存在名为【108小说网-军事】的解析器。"));

            result.Add(new AnalizerBase
            {
                Name = "108小说网-军事",
                BookNamePrefix = "",
                Encoding = Encoding.GetEncoding("GB2312"),
                SiteUrl = "http://www.book108.com/",
                BooksUrl = "http://www.book108.com/search.asp?flag=11&flag2=%CD%C6%C0%ED%D5%EC%CC%BD",
                BookRegex = "<a href='(?<url>.+?)' target='_blank' class='c'>(?<title>.+?)</a>",
                ChaptersUrl = "http://www.book108.com/{0}",//章节列表
                ChapterRegex = "<div class='bb'><a href='(?<url>.+?).html' target='_blank' title='(更新时间：.+?)'>(?<title>.+?)</a></div>",//章节正则（包含前言）
                ChapterRegexTag = new string[] { "url", "title" },
                ContentBaseUrl = "http://www.book108.com/{0}{1}.html",//正文页面
                ContentRegex = "(?<=<div class='detail' id='content'>).+?(?=</div>)",//正文正则 
                ContentClears = new Dictionary<string, string> {// 正文中要清理的垃圾
                    {"<P>"+"[１|1][0|O|０][8|８][小|尒][说|説]www[.|。]b[0|O|０][0|O|０]K[１|1][0|O|０][8|８].COM[更|鯁][新|噺]",""},
                    {"[(|（]"+"[１|1][0|O|０][8|８][小|尒][说|説]www[.|。]b[0|O|０][0|O|０]K[１|1][0|O|０][8|８].COM[更|鯁][新|噺]"+"[)|）]",""},
                    {"<div align='center'>.*?</div>", ""},
                    {"<a.+?>.+?</a>", ""}
                }
            });
            #endregion

            #region 108小说网 - 狼群
            if (result.Find(delegate(AnalizerBase bm) { return bm.Name == "108小说网-狼群"; }) != null) throw (new Exception("已存在名为【108小说网-狼群】的解析器。"));

            result.Add(new AnalizerBase
            {
                Name = "108小说网-狼群",
                BookNamePrefix = "",
                Encoding = Encoding.GetEncoding("GB2312"),
                SiteUrl = "http://www.book108.com/",
                ChaptersUrl = "http://www.book108.com/a/252/catalog.html",//章节列表
                ChapterRegex = "<div class='bb'><a href='(?<url>.+?).html' target='_blank' title='(更新时间：.+?)'>(?<title>.+?)</a></div>",//章节正则（包含前言）
                ChapterRegexTag = new string[] { "url", "title" },
                ContentBaseUrl = "http://www.book108.com/a/252/{0}.html",//正文页面
                ContentRegex = "(?<=<div class='detail' id='content'>).+?(?=</div>)",//正文正则 
                ContentClears = new Dictionary<string, string> {// 正文中要清理的垃圾
                    {"<P>"+"[１|1][0|O|０][8|８][小|尒][说|説]www[.|。]b[0|O|０][0|O|０]K[１|1][0|O|０][8|８].COM[更|鯁][新|噺]",""},
                    {"[(|（]"+"[１|1][0|O|０][8|８][小|尒][说|説]www[.|。]b[0|O|０][0|O|０]K[１|1][0|O|０][8|８].COM[更|鯁][新|噺]"+"[)|）]",""},
                    {"<div align='center'>.*?</div>", ""},
                    {"<a.+?>.+?</a>", ""}
                }
            });
            #endregion

            #region 狼图腾 - 狼图腾
            if (result.Find(delegate(AnalizerBase bm) { return bm.Name == "狼图腾 - 狼图腾"; }) != null) throw (new Exception("已存在名为【狼图腾 - 狼图腾】的解析器。"));

            result.Add(new AnalizerBase
            {
                Name = "狼图腾 - 狼图腾",
                BookNamePrefix = "",
                Encoding = Encoding.GetEncoding("GB2312"),
                SiteUrl = "http://www.langtuteng.com",
                ChaptersUrl = "http://www.langtuteng.com",//章节列表
                ChapterRegex = @"<a href=""(?<url>\d{3}).htm"">(?<title>.+?)</a>(\s*</span>)*</td>",//章节正则（包含前言）
                ChapterRegexTag = new string[] { "url", "title" },
                ContentBaseUrl = "http://www.langtuteng.com/{0}.htm",//正文页面
                ContentRegex = @"(?<=<td class=""ShowTitleBody"">).+?(?=</td>)",//正文正则 
                ContentClears = new Dictionary<string, string> {// 正文中要清理的垃圾
                }
            });
            #endregion

            #region 狼图腾 - 我的美女总裁
            if (result.Find(delegate(AnalizerBase bm) { return bm.Name == "狼图腾 - 我的美女总裁"; }) != null) throw (new Exception("已存在名为【狼图腾 - 我的美女总裁】的解析器。"));

            result.Add(new AnalizerBase
            {
                Name = "狼图腾 - 我的美女总裁",
                BookNamePrefix = "",
                Encoding = Encoding.GetEncoding("GB2312"),
                SiteUrl = "http://www.htzw.net/html/62/62799/",
                ChaptersUrl = "http://www.htzw.net/html/62/62799/",//章节列表
                ChapterRegex = "<a ((title=\".+?\" href=\"(?<url>\\d{8}).shtml)|(href=\"(?<url>\\d{8}).shtml\" title=\".+?))\">(?<title>.+?)</a>",//章节正则（包含前言）
                ChapterRegexTag = new string[] { "url", "title" },
                ContentBaseUrl = "http://www.htzw.net/html/62/62799/{0}.shtml",//正文页面
                ContentRegex = @"(?<=好看的女生小说).+?(?=<script)",//正文正则 
                ContentClears = new Dictionary<string, string>
                {// 正文中要清理的垃圾
                }
            });
            #endregion

            return result;
        }

        private static void CheckExist(List<AnalizerBase> aBMs, string siteName)
        {
            if (aBMs.Find(delegate(AnalizerBase bm) { return bm.Name == siteName; }) != null) 
                throw (new Exception(string.Format("已存在名为【{0}】的解析器。", siteName)));
        }
    }
}
